// modules/orderModules/components/orderItem/index.js
import {msgTip, msgTipAlert} from '@/utils/MsgTipApi';
import {removeOrder,cancelOrder,buyAgain} from '@/api/order'
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    orderItem:{},
    type:''
  },
  observers:{
    orderItem: function(data){
      let orderName = ""
      if(data.orderItemRespVoList.length > 1){
        for(let i = 0; i < data.orderItemRespVoList.length ; i++){
          let item = data.orderItemRespVoList[i];
          orderName+= item['goodName']
          if(i != data.orderItemRespVoList.length - 1){
            orderName+="/ "
          }
        }
      }
      this.setData({
        orderName,
      })
    },
    type: function(data){
      console.log('type',data);
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    orderName:"",
    orderStatusEnum:[
      {orderStatus:'0',type:'warring'},
      {orderStatus:'1',type:'danger'},
      {orderStatus:'2',type:'info'},
      {orderStatus:'3',type:'warring'},
      {orderStatus:'4',type:'info'},
      {orderStatus:'5',type:'warring'},
      {orderStatus:'6',type:'primary'},
      {orderStatus:'7',type:'warring'},
    ],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    formatPrice(data){
        return data.toFixed(2)
    },
  /**
   * 得到状态类型
   */
  getStatusType(event){
    console.log(event,'被调用');
    let type = ''
    this.data.orderStatusEnum.forEach(item =>{
      if(item['orderStatus'] == status){
        type = item['type']
      }
    })
    console.log(type);
    return type || 'warring';
  },
    /**
     * 删除订单
     */
    async delOrder(event){
      let {id} = event.currentTarget.dataset;
      let select = await msgTipAlert({title:'警告', content:'是否确认删除该订单', showCancel:true})
      if(select){
        let res = await removeOrder(id)
        if(res){
          this.triggerEvent('del',{})
        }
      }else{
        msgTip({title:'取消删除'})
      }
    },
    async cancelOrder(event){
      let {id,status} = event.currentTarget.dataset;
      if(status == 7){
        msgTip({title:'订单已取消'})
        return;
      }
      let select = await msgTipAlert({title:'提示', content:'是否取消该订单', showCancel:true})
      if(select){
        let res = await cancelOrder(id)
        this.triggerEvent('cancel',{})
      }else{
        msgTip({title:'取消操作'})
      }
    },
    /**
     * 前往订单详情
     */
    getOrderDetail(event){
      let {id} = event.currentTarget.dataset
      wx.navigateTo({
        url: `/modules/orderModules/pages/orderDetail/index?orderId=${id}`,
      })
    },
    goEvaluate(event){
      console.log('前往评价');
    },
    async buyAgain(event){
      let {id} = event.currentTarget.dataset;
      // 先将该订单中的产品加入到购物车中
      let res = await buyAgain(id);
      console.log(res);
      if(res){
        wx.reLaunch({
          url: '/pages/shoppingCart/shoppingCart',
        })
      }
    }
  },
  lifetimes:{
    created(){
      
    }
  }
})